<?php
	if (version_compare(phpversion(), '5.2.0', '<')) 
	{
		echo  'Cette version de PHP est incompatible';
		exit;
	}
    defined('APPLICATION_ENV') || define('APPLICATION_ENV', (getenv('APPLICATION_ENV') ? getenv('APPLICATION_ENV') : 'production'));    
    define('APPLICATION_PATH', realpath(dirname(__file__)) . '/');
    define('DIR_CONFIG', realpath(dirname(__file__)) . '/config/');
    define('DIR_LIB', realpath(dirname(__file__) . '/../../../lib') . '/');
	define('DIR_OPT', realpath(dirname(__file__) . '/options') . '/');
	define('DIR_CACHE', realpath(dirname(__file__) . '/cache') . '/');
	define('DIR_LOGS', realpath(dirname(__file__) . '/logs') . '/');
	define('LOG_FILE', DIR_LOGS . 'logs.log');
	define('DIR_HELPERS', realpath(dirname(__file__) . '/helpers') . '/');
	define('DIR_VIEWS', realpath(dirname(__file__) . '/views') . '/');
	define('DIR_TMP', realpath(dirname(__file__) . '/tmp') . '/');
	define('DIR_JDB', realpath(dirname(__file__) . '/db') . '/');
	define('DIR_HOOKS', realpath(dirname(__file__) . '/hooks') . '/');
	define('DIR_TRAD', realpath(dirname(__file__) . '/trad') . '/');
	define('DIR_CONTROLLERS', realpath(dirname(__file__) . '/controllers') . '/');
	define('DIR_MODELS', realpath(dirname(__file__) . '/models') . '/');
	define('DIR_IMG', realpath(dirname(__file__) . '/../../../www/assets/img') . '/');
	define('DIR_JS', realpath(dirname(__file__) . '/../../../www/assets/js') . '/');
	define('DIR_CSS', realpath(dirname(__file__) . '/../../../www/assets/css') . '/');
	define('DIR_FONTS', realpath(dirname(__file__) . '/../../../www/assets/css/fonts') . '/');
	
    
    if ('development' == APPLICATION_ENV)
    {
        error_reporting(E_ALL & ~E_NOTICE);
        ini_set('display_error', 1);
    }

	require_once(DIR_LIB . 'gp/functions.php');
    
    load('option');
    
    set_include_path(implode(PATH_SEPARATOR, array(
		realpath(APPLICATION_PATH . '/../../../lib'),
		get_include_path(),
	)));
    
    setvar('**start**', getMicrotime());
    
    zend();
    
    load('controller');
    load('captcha');
    load('db');
    load('app');
    load('debug');
    load('hook');
    load('jdb');
    load('mongo');
    load('model');
    load('dbcache');
    load('trad');
    
    $application = new Zend_Application(
        APPLICATION_ENV,
        DIR_CONFIG . 'zend.ini'
    );

    appsession();
    initCssForm();
    
    $config = parse_ini_file(DIR_CONFIG . APPLICATION_ENV . '.ini');
    setvar('**config**', $config);
    setvar('**module**', $module);
    
    date_default_timezone_set($config['timezone']);
    
    define('SESSION_NAME', $config['sessionName']);
    define('VERBOSE', $config['verbose']);
	define('DEBUG', $config['debug']);
	define('PAGE_DEFAULT', $config['defaultPage']);
	define('LNG_DEFAULT', $config['defaultLanguage']);
	define('CACHE_DB_NAME', $config['cacheDbName']);
	define('CACHE_DB_COLLECTION', $config['cacheDbCollection']);
	define('CACHE_DB_HOST', $config['cacheDbHost']);
	define('CACHE_DB_USER', $config['cacheDbUser']);
	define('CACHE_DB_PASSWORD', $config['cacheDbPassword']);
	define('CACHE_DB_CACHETIME', $config['cacheDbDuration']); // 2 heures
    
    $db = db();
    get_vars();
    purge();
    
    // cleanDbCache();
    // setDbCache('test', date("d/m/Y H:i:s"));
    // var_dump(getDbCache('test'));exit;
    
    $lng = getvarpost('lngto');
    if (null === $lng)
    {
        $lng = getoption('__lng__');
        if (null === $lng)
        {
            $lng = LNG_DEFAULT;
            setoption('__lng__', $lng);
        }
    }
    else
        setoption('__lng__', $lng);
    
    //traductions
    $trad = GP_jdb::getInstance('translations', $lng);
    
    $url_site = "http://" . $_SERVER["SERVER_NAME"] . dirname($_SERVER["SCRIPT_NAME"]);
	if ($url_site[strlen($url_site) - 1] == '\\')
		$url_site = substr($url_site, 0, -1);
	$url_site .= '/';
		
	if (strstr($url_site, '//'))
	{
		$url_site = _r('//', '/', $url_site);
		$url_site = _r(':/', '://', $url_site);
	}

    define('URL_SITE', $url_site);
    setvar('**urlSite**', URL_SITE);
	$page = getPage();
    define('__PAGE__', $page);
    display();
?>